import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class Solution {
    //等差数列划分II
    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][n];
        Map<Long, ArrayList<Integer>> hash = new HashMap<>();
        for(int i = 0; i < n; i++){
            long tmp = (long) nums[i];
            if(!hash.containsKey(tmp)){
                hash.put(tmp, new ArrayList<Integer>());
            }
            hash.get(tmp).add(i);
        }
        int ret = 0;
        for(int j = 2; j < n; j++){
            for(int i = 0; i < j; i++){
                long a = 2L*nums[i] - nums[j];
                if(hash.containsKey(a)){
                    for(int k: hash.get(a)){
                        if(k < i) dp[i][j] += (dp[k][i] + 1);
                        else break;
                    }
                }
                ret += dp[i][j];
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] nums = {7,7,7,7,7};
        Solution solution = new Solution();
        solution.numberOfArithmeticSlices(nums);
    }
}